package cn.jbt.util;

import jakarta.servlet.http.HttpServletRequest;

public class IpAddressUtil {

    public static String getIp(HttpServletRequest request) {

        // 通过 X-Forwarded-For 头部获取真实IP地址
        String ipAddress = request.getHeader("X-Forwarded-For");

        // 如果 X-Forwarded-For 头部为空，则通过 Proxy-Client-IP 头部获取
        if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("Proxy-Client-IP");
        }

        // 如果 Proxy-Client-IP 头部为空，则通过 WL-Proxy-Client-IP 头部获取
        if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("WL-Proxy-Client-IP");
        }

        // 如果 WL-Proxy-Client-IP 头部为空，则通过 RemoteAddr 获取
        if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getRemoteAddr();
        }

        return ipAddress;
    }
}
